package com.chinasoftware.civilaviation.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

@Controller
@RequestMapping(value="/admin")
public class PredictController {
    @ResponseBody
    @RequestMapping(value = "/predict", method = POST)
    public Map<String, Object> predict(String temperature, String flight, String cloud, String visible) {
        if(temperature ==""){
            temperature = "0";
        }
        System.out.println(temperature);
        System.out.println(flight);
        System.out.println(cloud);
        System.out.println(visible);

        Map<String, Object> resultMap = new HashMap<String, Object>();
        String url = "D:\\air\\Civil-Aviation-China-Software\\airplane-admin\\src\\main\\java\\com\\chinasoftware\\civilaviation\\python\\predict.py";

        String line = null;
        try {
            String[] args = new String[] { "python", url, temperature, flight,cloud,visible};
            Process proc = Runtime.getRuntime().exec(args);// 执行py文件
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));

            line = in.readLine();
            System.out.println(line);

            in.close();
            int re = proc.waitFor();
            System.out.println(re);

            resultMap.put("State", "true");
            resultMap.put("predict",line);

        } catch (IOException e) {
            resultMap.put("State", "false");
            e.printStackTrace();
        } catch (InterruptedException e) {
            resultMap.put("State", "false");
            e.printStackTrace();
        }

        return resultMap;
    }
}
